package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
//1.1分页条件查询护理项目 四个参数：项目名称name，状态status，页码pageNum，每页大小pageSize

    @Override
    public PageResponse<NursingProjectVo> selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
//1.1 设置分页
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> nursingProjects = nursingProjectMapper.selectByPage(name, status);
        return PageResponse.of(nursingProjects, NursingProjectVo.class);
    }

    //1.2 新增护理项目
    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {

        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        //校验是否重名
        int count = nursingProjectMapper.countByName(nursingProject.getName());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_INSERT_FAIL_INSERT_NURSING_PROJECT_NAME);
        }
        // 价格，排序校验
        BigDecimal number = BigDecimal.valueOf(999999999.99);
        BigDecimal price = nursingProject.getPrice();
        if (price.compareTo(number)>0){
            throw new RuntimeException("护理费用不能超过999999999.99");
        }
        Integer orderNo = nursingProject.getOrderNo();
        if (orderNo>100){
            throw new RuntimeException("排序号不能超过100");
        }

        nursingProjectMapper.insert(nursingProject);
    }
    // 1.3 根据id查询项目
    @Override
    public NursingProjectVo findById(Long id) {
        // 1.3.1 根据id校验项目是否存在
        int count = nursingProjectMapper.queryNPById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_QUERY_FAIL_PROJECT_ID); }
        // 1.3.2 项目存在 根据id查询项目
        NursingProject nursingProject = nursingProjectMapper.findById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }
    // 1.4 修改项目
    @Override
    public void update(NursingProjectDto nursingProjectDto) {

        // 1.4.1校验是否存在
        int count = nursingProjectMapper.queryNPById(nursingProjectDto.getId());
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_QUERY_FAIL_PROJECT_ID); }
        // id 存在，可以进行修改
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        // 价格，排序校验
        BigDecimal number = BigDecimal.valueOf(999999999.99);
        BigDecimal price = nursingProject.getPrice();
        if (price.compareTo(number)>0){
            throw new RuntimeException("护理费用不能超过999999999.99");
        }
        Integer orderNo = nursingProject.getOrderNo();
        if (orderNo>100){
            throw new RuntimeException("排序号不能超过100");
        }
        nursingProjectMapper.update(nursingProject);

    }

    // 1.5 删除护理项目信息
    @Override
    public void deleteById(Long id) {
        int count = nursingProjectMapper.queryNPById(id);
        if (count == 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_DELETE_FAIL_PROJECT_ID); }
        // 必须判断状态是否禁用，禁用才能删除
        if (nursingProjectMapper.findById(id).getStatus() == 1) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_DELETE_FAIL_PROJECT_STATUS); }
        nursingProjectMapper.deleteById(id);

    }
    // 1.6 启动或禁用
    @Override
    public void isEnable(Long id, Integer status) {
        nursingProjectMapper.isEnable(id,status);
    }

    // 1.7 查询所有护理项目
    @Override
    public List<NursingProjectVo> selectAll() {

        return nursingProjectMapper.selectAll();
    }
}
